package com.wangxiang.shop.member.controller;

import com.wangxiang.common.utils.MyStringUtile;
import com.wangxiang.common.utils.R;
import com.wangxiang.shop.member.entity.MemberEntity;
import com.wangxiang.shop.member.entity.ShopCartEntity;
import com.wangxiang.shop.member.feign.OrderFeign;
import com.wangxiang.shop.member.feign.ProductFeign;
import com.wangxiang.shop.member.service.MemberService;
import com.wangxiang.shop.member.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("member")
public class PayController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    OrderFeign orderFeign;
    @Autowired
    ProductFeign productFeign;
    /**
     * 支付接口
     */
    @RequestMapping("pay")
    public R payByCart(@RequestBody HashMap<String , Object> map){
//        System.out.println(map);
        if (map.get("payPassword")==null|map.get("payPassword")==""){
            return R.error("密码为空，请填写密码");
        }
        try {
            Integer.valueOf(map.get("payPassword").toString());
        }catch (NumberFormatException n){

            return R.error("你输入的不是数值");
        }


        //校验密码
        if (map.get("userId")!=null){
            int userId = (int) map.get("userId");
            MemberEntity info = memberService.info(userId);
            if (info!=null){
                int payPassword = Integer.valueOf(map.get("payPassword").toString()) ;
                if (payPassword!=info.getPayPassword()){
                    return R.error("密码错误");
                }
            }
           if (map.get("price")!=null){
               Double price = Double.valueOf(map.get("price").toString());
               if (info.getBalance()<price){
                   return R.error("余额不足");
               }else {
                   //余额充足抵扣
                   double balance = info.getBalance()-price;
                   memberService.balance(info.getUserId(),balance);
               }

           }

        }
        List<ShopCartEntity> shopCartEntities = new ArrayList<>();
        //查询订单数据
        if (map.get("shopIdList")!=null){
            MyStringUtile myStringUtile = new MyStringUtile();
            List<Integer> cartIdList = myStringUtile.skuIdStrChangeSkuIdList(map.get("shopIdList").toString());
            for (int i = 0; i < cartIdList.size(); i++) {
                ShopCartEntity info = shopCartService.info(cartIdList.get(i));
                if (map.get("receiptId")!=null){
                    int receiptId = Integer.valueOf(map.get("receiptId").toString());
                    info.setReceiptId(receiptId);
                }
                //0 有效 ， 1已购买  ， 商品已失效
                if (info.getStatus()==0){

                    shopCartEntities.add(info);

                }
            }
        }
        //远程接口调用，订单模块
        List<Integer> cartIdList = orderFeign.addByCart(shopCartEntities);
//        System.out.println(cartIdList);
        //购物车购买方式返回cartIdList，把订单表插如成功的购物车记录状态置为1 已购买
        for (Integer integer : cartIdList) {
            shopCartService.updateStatus(integer);
        }
        //调用商品模块改变库存信息
        List<HashMap<String, Integer>> hashMaps = shopCartService.updateWare(cartIdList);
        productFeign.updateWare(hashMaps);
        return R.ok().put("msg","感谢你对本商城的支持");
    }

    /**
     * 立即支付
     * @param map
     * @return
     */
    @RequestMapping("paynow")
    public R payNow(@RequestBody HashMap<String ,Object> map){
//        System.out.println("立即购买数据"+map);
        //支付密码校验
        if (map.get("payPassword")==null|map.get("payPassword")==""){
            return R.error("密码为空，请填写密码");
        }
        try {
            Integer.valueOf(map.get("payPassword").toString());
        }catch (NumberFormatException n){

            return R.error("你输入的不是数值");
        }
        if (map.get("userId")!=null){
            int userId = (int) map.get("userId");
            MemberEntity info = memberService.info(userId);
            if (info!=null){
                int payPassword = Integer.valueOf(map.get("payPassword").toString()) ;
                if (payPassword!=info.getPayPassword()){
                    return R.error("密码错误");
                }
            }
            if (map.get("price")!=null){
                Double price = Double.valueOf(map.get("price").toString());
                if (info.getBalance()<price){
                    return R.error("余额不足");
                }else {
                    //余额充足抵扣
                    double balance = info.getBalance()-price;
                    memberService.balance(info.getUserId(),balance);
                }
            }
        }
        //商品模块拉取skuId,改变库存
        HashMap<String, String> productMap = new HashMap<>();
        productMap.put("skuIdList", map.get("skuIdList").toString());
        productMap.put("num",map.get("num").toString());
        if (map.get("style")!=null){
            productMap.put("style",map.get("style").toString());
        }
        if (map.get("size")!=null){
            productMap.put("size",map.get("size").toString());
        }
//        System.out.println("数据"+productMap);
        //查询skuId
        int skuId = productFeign.paynow(productMap);
        //回填订单表，在订单表落实例，立即购买的cartId置为0标识购买方式为立即购买
        HashMap<String, Integer> order = new HashMap<>();
        map.put("skuId",skuId);
        map.put("cartId",0);
        int i = orderFeign.addByPayNow(map);
        if (i>0){
            HashMap<String, Integer> productWare = new HashMap<>();
            productWare.put("skuId",skuId);
            productWare.put("num",Integer.valueOf(map.get("num").toString()));
            //订单表回填数据成功，改变商品模块商品库存
            int i1 = productFeign.updateWareByPayNow(productWare);
            if (i1>0){
                return R.ok().put("msg","购买成功");
            }
        }

        return R.error("购买失败，系统错误");

    }

}
